﻿<CodeSnippetTabbed DocLink="https://docs.devexpress.com/Blazor/DevExpress.Blazor.DxDataGrid-1.CustomData">
    <CodeSnippetTabPage Text="Razor">
        @(@"@using DevExtreme.AspNet.Data
@using DevExtreme.AspNet.Data.ResponseModel
@using System.Threading
@using System.Threading.Tasks
@using System.Net.Http
@using System.Text.Json

<DxDataGrid T=""@WebApiOrder""
            CustomData=""@LoadOrderData""
            ShowFilterRow=""true"">

    <Columns>
        <DxDataGridComboBoxColumn T=""@(WebApiCustomerLookup)"" TextFieldName=""Text"" ValueFieldName=""Value""
                                  CustomData=""@LoadCustomerData"" Field=""@nameof(WebApiOrder.CustomerID)"" Width=""300px"" />
        <DxDataGridDateEditColumn Field=""@nameof(WebApiOrder.OrderDate)"" Width=""180px"" />
        <DxDataGridSpinEditColumn Field=""@nameof(WebApiOrder.Freight)"" DisplayFormat=""c"" Width=""180px"" />
        <DxDataGridColumn Field=""@nameof(WebApiOrder.ShipCountry)"" Width=""210px"" />
        <DxDataGridComboBoxColumn T=""@(WebApiShipperLookup)"" TextFieldName=""Text"" ValueFieldName=""Value""
                                  CustomData=""@LoadShipmentData"" Field=""@nameof(WebApiOrder.ShipVia)"" Width=""220px"" />
    </Columns>
</DxDataGrid>
@code {
    [Inject] protected HttpClient Client { get; set; }

    protected async Task<LayoutLoadResult> LoadOrderData(DataSourceLoadOptionsBase options, CancellationToken cancellationToken)
        => await LoadCustomData(""https://js.devexpress.com/Demos/NetCore/api/DataGridWebApi/Orders"", options, cancellationToken);
    protected async Task<LayoutLoadResult> LoadCustomerData(DataSourceLoadOptionsBase options, CancellationToken cancellationToken)
        => await LoadCustomData(""https://js.devexpress.com/Demos/NetCore/api/DataGridWebApi/CustomersLookup"", options, cancellationToken);
    protected async Task<LayoutLoadResult> LoadShipmentData(DataSourceLoadOptionsBase options, CancellationToken cancellationToken)
        => await LoadCustomData(""https://js.devexpress.com/Demos/NetCore/api/DataGridWebApi/ShippersLookup"", options, cancellationToken);

    protected async Task<LayoutLoadResult> LoadCustomData(string dataEndpointUri, DataSourceLoadOptionsBase options, CancellationToken cancellationToken)
    {
        using var response = await Client.GetAsync(options.ConvertToGetRequestUri(dataEndpointUri), cancellationToken);
        response.EnsureSuccessStatusCode();
        using var responseStream = await response.Content.ReadAsStreamAsync();
        return await JsonSerializer.DeserializeAsync<LoadResult>(responseStream, cancellationToken: cancellationToken);
    }
}")
    </CodeSnippetTabPage>
    <CodeSnippetTabPage Text="Data Objects" Language="csharp">
        @(@"using System;

public class WebApiOrder {
    public string CustomerID { get; set; }
    public DateTime OrderDate { get; set; }
    public decimal Freight { get; set; }
    public string ShipCountry { get; set; }
    public int ShipVia { get; set; }
}

public class WebApiCustomerLookup {
    public string Text { get; set; }
    public string Value { get; set; }
}

public class WebApiShipperLookup {
    public string Text { get; set; }
    public int Value { get; set; }
}
")
    </CodeSnippetTabPage>
    <CodeSnippetTabPage Text="Startup" Language="csharp">
        @(@"using System.Net.Http;

public class Startup {
    // ...
    public void ConfigureServices(IServiceCollection services) {
        // ...
        services.AddHttpClient<HttpClient>(ConfigureHttpClient);
        services.AddScoped<HttpClient>(serviceProvider => serviceProvider.GetService<IHttpClientFactory>().CreateClient());

        static void ConfigureHttpClient(HttpClient httpClient) {
            httpClient.DefaultRequestHeaders.Add(""Accept"", ""application / json"");
        };
    }
}")
    </CodeSnippetTabPage>
    <CodeSnippetTabPage Text="Web API Controller" Language="csharp">
        @(@"using DevExtreme.AspNet.Data;
using DevExtreme.AspNet.Mvc;
// ...

[Route(""api /[controller] /[action]"")]
public class DataGridWebApiController : Controller
{
    NorthwindContext _nwind;

    [HttpGet]
    public object Orders(DataSourceLoadOptions loadOptions) {
        return DataSourceLoader.Load(_nwind.Orders, loadOptions);
    }
    // ...
}
")
    </CodeSnippetTabPage>
</CodeSnippetTabbed>
